package com.agentfactory.teleoreactive.interpreter.mentalstate;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.agentfactory.logic.update.lang.IFormula;
import com.agentfactory.logic.update.lang.IMPLIES;
import com.agentfactory.logic.update.lang.Predicate;
import com.agentfactory.logic.update.reasoner.IQueryable;
import com.agentfactory.logic.update.util.DefaultLogicPresenter;
import com.agentfactory.teleoreactive.interpreter.Utilities;

public class PerceptionTower implements IQueryable {
	private List<IMPLIES> rules;
	private Map<String, List<IFormula>> ruleMap;
	
	public PerceptionTower(){
		rules = new LinkedList<IMPLIES>();
		ruleMap = new HashMap<String, List<IFormula>>(); 
	}
    public boolean hasType(String type) {
	    return true;
    }
	
	public void addRule(IMPLIES rule){
		String key= "";
    	if(Predicate.class.isInstance(rule.result())){
    		key = ((Predicate)rule.result()).functor();
    	}
		List<IFormula> list = ruleMap.get(key);
		if (list == null) {
			list = new LinkedList<IFormula>();
			list.add(rule);
			ruleMap.put(key, list);
		} else {
			for (IFormula r: list) {
				if (r.equals(rule)) {
					return;
				}
			}
			list.add(rule);
			ruleMap.put(key, list);
		}
		
		rules.add(rule);
	}
	
    public List<IFormula> query(IFormula query) {
    	DefaultLogicPresenter p = new DefaultLogicPresenter();
    	String key = ((Predicate)query).functor();
			List<IFormula> rules = ruleMap.get(key);
			if (rules != null) {
				return rules;
			}
		return new LinkedList<IFormula>();
    }

}
